package id.agung.wonogirimaps;

import java.util.List;

import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockActivity;

import id.agung.wonogirimaps.entity.Category;
import id.agung.wonogirimaps.util.JSONAdapter;
import id.agung.wonogirimaps.util.Utils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CategoryActivity extends SherlockActivity implements
		OnItemClickListener
{
	public static final String		TAG_CAT	= "category";
	private static final String		TAG		= CategoryActivity.class.getName();

	private JSONAdapter				json;
	private ArrayAdapter<Category>	adapter;
	private ListView				listCategory;
	private ProgressDialog			pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_category);

		getSupportActionBar().setTitle("Category");
		getSupportActionBar().setIcon(android.R.color.transparent);

		json = new JSONAdapter();

		listCategory = (ListView) findViewById(R.id.listCategory);
		listCategory.setOnItemClickListener(this);

		/**
		 * Jalankan asynctask
		 */
		Utils.TRACE(TAG, Utils.API_CATEGORY);
		new AsycnCategory().execute(Utils.API_CATEGORY);
	}

	/**
	 * Inner class untuk mengambil data category secara asynchronous
	 * 
	 * @author pratama
	 * 
	 */
	private class AsycnCategory extends AsyncTask<String, Void, List<Category>>
	{
		/**
		 * Method ini dijalankan secara background
		 */
		@Override
		protected List<Category> doInBackground(String... arg0)
		{
			JSONObject obj = json.getJSONFromURL(arg0[0]);

			return json.getAllCategory(obj);
		}

		/**
		 * Ketika method doInBackground selesai dijalankan, maka selanjutnya
		 * asynctask akan menjalankan method onpostexecute
		 */
		@Override
		protected void onPostExecute(List<Category> result)
		{
			super.onPostExecute(result);
			pDialog.dismiss();
			adapter = new ArrayAdapter<Category>(CategoryActivity.this,
					android.R.layout.simple_list_item_1, result);
			listCategory.setAdapter(adapter);
		}

		/**
		 * method onPre adalah method yang pertama kali dijalankan oleh class
		 * AsyncTask
		 */
		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
			pDialog = new ProgressDialog(CategoryActivity.this);
			pDialog.setMessage("Loading");
			pDialog.setCancelable(true);
			pDialog.show();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int loc, long id)
	{
		Bundle b = new Bundle();
		b.putInt(TAG_CAT, adapter.getItem(loc).getId());

		Intent i = new Intent(this, LocationActivity.class);
		i.putExtras(b);
		startActivity(i);
	}

	/**
	 * method yang digunakan untuk menangani ketika user menekan tombol back
	 */
	@Override
	public void onBackPressed()
	{
		super.onBackPressed();
		startActivity(new Intent(this, MainActivity.class));
		finish();
	}

}
